﻿#include "OperatorsGeometryExport.h"

#include "GUIFrame/MainWindow.h"
#include "GUIFrame/CentralWidget.h" 
#include "OperatorsInterface/GraphEventOperator.h"

#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppSettings.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"
#include "FITK_Kernel/FITKCore/FITKThreadPool.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoInterfaceFactory.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeoModelExport.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKGeoCommandList.h"
#include "FITK_Interface/FITKInterfaceGeometry/FITKAbsGeomTools.h"

#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsSEAModel.h"
#include "FITK_Component/FITKAcousticsModel/FITKAcousticsDiscNodeList.h"

#include <QFileDialog>
#include <QApplication>

namespace ModelOper
{

    bool OperatorsGeometryExport::execGUI()
    {
        //获取工作路径
        QString workDir = "";
        if (FITKAPP->getAppSettings())
            workDir = FITKAPP->getAppSettings()->getWorkingDir();
        if (workDir.isEmpty()) workDir = QApplication::applicationDirPath();
        //输入导出文件路径
        QFileDialog dlg(FITKAPP->getGlobalData()->getMainWindow(), tr("Export Geometry"), workDir, tr("File(*.step)"));
        dlg.setOption(QFileDialog::DontUseNativeDialog);
        dlg.setAcceptMode(QFileDialog::AcceptSave);
        if (QDialog::Accepted != dlg.exec()) return false;
        //获取导出文件路径
        QString suffixFilter = dlg.selectedNameFilter();
        QStringList files = dlg.selectedFiles();
        if (suffixFilter.isEmpty() || files.isEmpty())
            return false;
        QString suffixWithHalfBrak = suffixFilter.split("*").last().trimmed();
        QString realSuffix = suffixWithHalfBrak.mid(0, suffixWithHalfBrak.length() - 1);
        QString fileName = files.first();
        if (!fileName.contains(realSuffix))
            fileName.append(realSuffix);
        if (fileName.isEmpty())return false;
        //设置文件路径
        this->setArgs("FileName", fileName);
        return true;
    }

    bool OperatorsGeometryExport::execProfession()
    {
        //获取读入文件路径
        QString fileName;
        this->argValue<QString>("FileName", fileName);
        if (fileName.isEmpty()) return false;

        Interface::FITKGeoCommandList* geometryData = FITKAPP->getGlobalData()->getGeometryData<Interface::FITKGeoCommandList>();
        if (geometryData == nullptr) return false;
        Interface::FITKInterfaceGeometryFactory* geoFactory = Interface::FITKInterfaceGeometryFactory::getInstance();
        if (geoFactory == nullptr)return false;
        //创建导入的几何数据对象
        Interface::FITKAbsGeoModelExport* geoObj = geoFactory->createCommandT<Interface::FITKAbsGeoModelExport>(Interface::FITKGeoEnum::FITKGeometryComType::FGTExport);
        if (geoObj == nullptr)return false;
        geoObj->setFileName(fileName);
        //更新几何数据
        bool result = geoObj->update();
        //释放数据
        delete geoObj;

        // 输出日志。
        if (result)
        {
            AppFrame::FITKMessageNormal(QString("Export Geometry File Succeed !!! %1").arg(fileName));
            //保存执行的脚本
            QString scr = QString("Global.Files.ExportGeoFile('%1')").arg(fileName);
            this->saveScript(scr);
        }
        else
            AppFrame::FITKMessageError(QString("Export Geometry File Err !!! %1").arg(fileName));
        this->clearArgs();
        return true;
    }
}

